home *** CD-ROM | disk | FTP | other *** search
- { list.pas -- Demonstrate list-box control }
-
- program List;
-
- {$R list.res}
-
- uses WinTypes, WinProcs, WObjects, Strings;
-
- const
-
- id_Menu = 100; { Menu resource ID }
- cm_Dialog = 101; { Dialog-command ID }
- id_Dialog = 200; { Dialog resource ID }
-
- id_Add = 101; { Dialog control ID values }
- id_Delete = 102;
- id_Input = 103;
- id_Listbox = 104;
-
- inputLen = 40; { Maximum length of input edit control }
-
- type
-
- PListDialog = ^ListDialog;
- ListDialog = object(TDialog)
- SelPointer: PChar;
- constructor Init(AParent: PWindowsObject; AName: PChar; P: PChar);
- procedure SetupWindow; virtual;
- procedure IDAdd(var Msg: TMessage);
- virtual id_First + id_Add;
- procedure IDDelete(var Msg: TMessage);
- virtual id_First + id_Delete;
- procedure Ok(var Msg: TMessage);
- virtual id_First + id_Ok;
- end;
-
- ListApplication = object(TApplication)
- procedure InitMainWindow; virtual;
- end;
-
- PListWindow = ^ListWindow;
- ListWindow = object(TWindow)
- Selection: array[0 .. inputLen] of Char;
- constructor Init(AParent: PWindowsObject; ATitle: PChar);
- procedure CMDialog(var Msg: TMessage);
- virtual cm_First + cm_Dialog;
- end;
-
- {- Add to list box identified by CrtlID in dialog window with the
- handle HDlg a new string addressed by P }
- procedure SetList(HDlg: HWnd; CtrlID: Word; P: PChar);
- begin
- SendDlgItemMessage(HDlg, CtrlID, lb_AddString, 0, LongInt(P))
- end;
-
-
- { ListDialog }
-
- {- Initialize ListDialog instance }
- constructor ListDialog.Init(AParent: PWindowsObject; AName: PChar;
- P: PChar);
- begin
- TDialog.Init(AParent, AName);
- SelPointer := P { Save pointer to caller's char array }
- end;
-
- {- Prepare ListDialog window. Insert list strings. }
- procedure ListDialog.SetupWindow;
- begin
- TDialog.SetupWindow;
- SendDlgItemMsg(id_Input, em_LimitText, inputLen, 0);
- SetList(HWindow, id_Listbox, 'Apples');
- SetList(HWindow, id_Listbox, 'Peaches');
- SetList(HWindow, id_Listbox, 'Pumpkin');
- SetList(HWindow, id_Listbox, 'Pie');
- end;
-
- {- Respond to Add button--Add text to list box }
- procedure ListDialog.IDAdd(var Msg: TMessage);
- var
- Buffer: array[0 .. inputLen] of Char;
- begin
- Buffer[0] := Chr(0);
- SendDlgItemMessage(HWindow, id_Input, wm_GetText, inputLen,
- LongInt(@Buffer));
- if StrLen(Buffer) > 0 then
- begin
- SetList(HWindow, id_Listbox, Buffer);
- SendDlgItemMessage(HWindow, id_Input, em_SetSel, 0,
- MakeLong(32767, 0))
- end
- end;
-
- {- Respond to Delete button--Delete selected list item }
- procedure ListDialog.IDDelete(var Msg: TMessage);
- var
- Item: Word; { Selected listbox-item index }
- begin
- Item := SendDlgItemMessage(HWindow, id_Listbox, lb_GetCurSel, 0, 0);
- if Item <> lb_Err then
- SendDlgItemMessage(HWindow, id_Listbox, lb_DeleteString, Item, 0)
- end;
-
- {- Respond to Ok button; Pass selected item back to caller. }
- procedure ListDialog.Ok(var Msg: TMessage);
- var
- Item: Word; { Selected listbox-item index }
- begin
- Item := SendDlgItemMessage(HWindow, id_Listbox, lb_GetCurSel, 0, 0);
- if Item <> lb_Err then
- SendDlgItemMessage(HWindow, id_Listbox, lb_GetText, Item,
- LongInt(SelPointer))
- else
- SelPointer[0] := Chr(0); { No selection }
- TDialog.Ok(Msg);
- end;
-
-
- { ListApplication }
-
- {- Initialize ListApplication object's window }
- procedure ListApplication.InitMainWindow;
- begin
- MainWindow := New(PListWindow, Init(nil, 'List'))
- end;
-
-
- { ListWindow }
-
- {- Construct ListWindow object }
- constructor ListWindow.Init(AParent: PWindowsObject; ATitle: PChar);
- begin
- TWindow.Init(AParent, ATitle);
- Attr.Menu := LoadMenu(HInstance, PChar(id_Menu));
- Selection[0] := Chr(0)
- end;
-
- {- Execute Menu:Dialog command }
- procedure ListWindow.CMDialog(var Msg: TMessage);
- begin
- Application^.ExecDialog(New(PListDialog,
- Init(@Self, PChar(id_Dialog), Selection)));
- if StrLen(Selection) <> 0 then
- MessageBox(HWindow, Selection, 'Selected Item', mb_Ok)
- end;
-
- var
-
- ListApp: ListApplication;
-
- begin
- ListApp.Init('ListApp');
- ListApp.Run;
- ListApp.Done
- end.
-
-
- {--------------------------------------------------------------
- Copyright (c) 1991 by Tom Swan. All rights reserved.
- Revision 1.00 Date: 3/20/1991
- ---------------------------------------------------------------}
-